package cn.tedu.supermarket.controller;

import cn.tedu.supermarket.entity.RegUser;
import cn.tedu.supermarket.mapper.UserMapper;
import cn.tedu.supermarket.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class RegController {
    @Autowired
    private EmailService emailService;
    @Autowired
    private UserMapper userMapper;

    @RequestMapping(value = "/regemail",produces = "text/html;charset=utf-8")
    public void start(String mailIndex){
        emailService.email(mailIndex);
    }

    @RequestMapping(value = "/checkreg")
    public ModelAndView checkreg(RegUser ruser, ModelMap map) {
        System.out.println("checkreg验证开始");
        try {
            System.out.println(emailService.getCaptcha()+","+ruser.getCaptcha());
            if (emailService.getCaptcha().equals(ruser.getCaptcha())){
                System.out.println("验证码正确,注册开始");
                System.out.println(ruser);
                int b = userMapper.addBuyUser(ruser.getUsername(),ruser.getPassword2(),ruser.getMailIndex());
                if (b>0) {
                    System.out.println("注册成功");
                    return new ModelAndView("buy_user/buserLogin");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.put("txt","验证码错误");
        System.out.println("验证码错误");
        return new ModelAndView("buy_user/buserReg");
    }


    @RequestMapping(value = "/selUserReg")
    public ModelAndView suserRegreg(RegUser ruser, ModelMap map) {
        System.out.println("验证开始");
        try {
            System.out.println(emailService.getCaptcha()+","+ruser.getCaptcha());
            if (emailService.getCaptcha().equals(ruser.getCaptcha())){
                System.out.println("验证码正确,注册开始");
                System.out.println(ruser);
                int b = userMapper.addSelUser(ruser.getUsername(),ruser.getPassword2(),ruser.getMailIndex());
                if (b>0) {
                    System.out.println("注册成功");
                    return new ModelAndView("sel_user/suserLogin");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.put("txt","验证码错误");
        System.out.println("验证码错误");
        return new ModelAndView("sel_user/suserReg");
    }
}
